#include "sys.h"
#include "delay.h"
#include "usart.h"
#include "led.h"
#include "key.h"
#include <string.h>

// 波形类型枚举
typedef enum {
    WAVE_SINE = 0,
    WAVE_TRIANGLE,
    WAVE_SQUARE,
    WAVE_SAWTOOTH
} WaveType;

#include <stdlib.h> // 包含atoi和atof函数声明
#include "lcd.h"    // 包含LCD函数声明

// 全局参数结构体
typedef struct {
    u16 times;
    u8 led_status;
    u8 key_status;
} SystemParams;

SystemParams sys_params = {0}; // 定义并初始化sys_params

// 波形参数全局变量
u8 g_wave_type = WAVE_SINE;  // 默认正弦波
u16 g_amp = 1000;            // 默认1.0V (1000对应1.0V)
u16 g_freq = 100;            // 默认100Hz

// 函数原型声明
void show_parameters(void);

// 命令解析函数
void parse_command(char* cmd) {
    if(strcmp(cmd, "led on") == 0) {
        LED0 = 0;
        sys_params.led_status = 1;
        printf("LED turned ON\r\n");
    } 
    else if(strcmp(cmd, "led off") == 0) {
        LED0 = 1;
        sys_params.led_status = 0;
        printf("LED turned OFF\r\n");
    }
    else if(strcmp(cmd, "status") == 0) {
        show_parameters();
    }
    else if(strncmp(cmd, "wave ", 5) == 0) {
        char* wave_type = cmd + 5;
        if(strcmp(wave_type, "sine") == 0) {
            g_wave_type = 0;
            printf("Waveform set to Sine\r\n");
        }
        else if(strcmp(wave_type, "triangle") == 0) {
            g_wave_type = 1;
            printf("Waveform set to Triangle\r\n");
        }
        else if(strcmp(wave_type, "square") == 0) {
            g_wave_type = 2;
            printf("Waveform set to Square\r\n");
        }
        else if(strcmp(wave_type, "sawtooth") == 0) {
            g_wave_type = 3;
            printf("Waveform set to Sawtooth\r\n");
        }
        else {
            printf("Invalid waveform type. Available: sine, triangle, square, sawtooth\r\n");
        }

      }
    else if(strncmp(cmd, "freq ", 5) == 0) {
        u16 new_freq = atoi(cmd + 5);
        if(new_freq >= 100 && new_freq <= 1000) {
            g_freq = new_freq;
            printf("Frequency set to %dHz\r\n", new_freq);
            LCD_ShowxNum(70,230,new_freq,4,16,0);
            LCD_ShowString(114,230,200,16,16,"   ");
        } else {
            printf("Frequency must be between 100-1000Hz\r\n");
        }
    }
    else if(strncmp(cmd, "amp ", 4) == 0) {
        float new_amp = atof(cmd + 4);
        if(new_amp >= 0 && new_amp <= 3.3) {
            g_amp = (u16)(new_amp * 1000);
            printf("Amplitude set to %.1fV\r\n", new_amp);
            LCD_ShowxNum(70,250,g_amp/1000,1,16,0);
            LCD_ShowxNum(86,250,(g_amp%1000)/100,1,16,0);
            LCD_ShowString(102,250,200,16,16,"    ");
        } else {
            printf("Amplitude must be between 0-3.3V\r\n");
        }
    }
    else {
        printf("Unknown command. Available commands:\r\n");
        printf("led on/off - Control LED\r\n");
        printf("status - Show system parameters\r\n");
        printf("wave [type] - Set waveform (sine/triangle/square/sawtooth)\r\n");
        printf("freq [value] - Set frequency (100-1000Hz)\r\n");
        printf("amp [value] - Set amplitude (0-3.3V)\r\n");
    }
}

// 参数显示函数
void show_parameters() {
    const char* wave_names[] = {"Sine", "Triangle", "Square", "Sawtooth"};
    
    printf("\r\n==== System Parameters ====\r\n");
    printf("System uptime: %d\r\n", sys_params.times);
    printf("LED status: %s\r\n", sys_params.led_status ? "ON" : "OFF");
    printf("Key status: %d\r\n", sys_params.key_status);
    printf("Waveform: %s\r\n", wave_names[g_wave_type]);
    printf("Frequency: %dHz\r\n", 100); // 需要添加频率变量
    printf("Amplitude: %.1fV\r\n", g_amp/1000.0);
    printf("==========================\r\n");
}
#include "lcd.h"
#include "sdram.h"
#include "usmart.h"
#include "adc.h"
#include "dac.h"
#include "timer.h"
/************************************************
 ALIENTEK ������STM32F429������ʵ��21
 DACʵ��-HAL�⺯����
 ����֧�֣�www.openedv.com
 �Ա����̣�http://eboard.taobao.com  
 ��ע΢�Ź���ƽ̨΢�źţ�"����ԭ��"����ѻ�ȡSTM32���ϡ�
 �������������ӿƼ����޹�˾  
 ���ߣ�����ԭ�� @ALIENTEK
************************************************/

int main(void)
{
 	u8 t=0;	 
	u8 key;	
    HAL_Init();                     //��ʼ��HAL��   
    Stm32_Clock_Init(360,25,2,8);   //����ʱ��,180Mhz
    delay_init(180);                //��ʼ����ʱ����
    uart_init(115200);              //��ʼ��USART
    usmart_dev.init(90); 		    //��ʼ��USMART	
    LED_Init();                     //��ʼ��LED 
    KEY_Init();                     //��ʼ������
    SDRAM_Init();                   //��ʼ��SDRAM
    LCD_Init();                     //��ʼ��LCD
    MY_ADC_Init();                  //��ʼ��ADC1
    DAC1_Init();                    //��ʼ��DAC1
    WaveTable_Init();               //��ʼ������
    
    // TIM3 initialization for DAC trigger
    TIM3_Init(179, 0);
    
	POINT_COLOR=RED; 
	LCD_ShowString(30,50,200,16,16,"Apollo STM32F4/F7");	
	LCD_ShowString(30,70,200,16,16,"signal-generator");	
	LCD_ShowString(30,90,200,16,16,"the first set");
	LCD_ShowString(30,110,200,16,16,"2025/6/10");	 
	LCD_ShowString(30,130,200,16,16,"WK_UP:+  KEY1:-");	 
	POINT_COLOR=BLUE;  	 
    
    // 使用全局变量控制波形参数
    const u16 MIN_FREQ = 100;   // 最小频率100Hz
    const u16 MAX_FREQ = 1000;  // 最大频率1kHz
    const u16 FREQ_STEP = 100;  // 步进100Hz
    
    LCD_ShowString(30,210,200,16,16,"Wave Type: Sine");
    LCD_ShowString(30,230,200,16,16,"Freq: 100Hz");
    LCD_ShowString(30,250,200,16,16,"Amp: 1.0V");
    
    while(1)
    {
        t++;
        key=KEY_Scan(0);
        sys_params.times++;
        
        // 串口数据处理
        if(USART_RX_STA&0x8000)
        {                       
            u16 len=USART_RX_STA&0x3fff;
            USART_RX_BUF[len] = '\0'; // 添加字符串结束符
            parse_command((char*)USART_RX_BUF); // 解析命令
            USART_RX_STA=0;
        }
        
        // 波形选择
        if(key==KEY0_PRES)
        {
            g_wave_type = (g_wave_type + 1) % 4;
            switch(g_wave_type)
            {
                case 0: LCD_ShowString(30,210,200,16,16,"Wave Type: Sine    "); break;
                case 1: LCD_ShowString(30,210,200,16,16,"Wave Type: Triangle"); break;
                case 2: LCD_ShowString(30,210,200,16,16,"Wave Type: Square  "); break;
                case 3: LCD_ShowString(30,210,200,16,16,"Wave Type: Sawtooth"); break;
            }
        }
        
        // 频率调节
        if(key==WKUP_PRES)
        {
            if(g_freq < MAX_FREQ) g_freq += FREQ_STEP; // 增加频率
            LCD_ShowxNum(70,230,g_freq,4,16,0);
            LCD_ShowString(114,230,200,16,16,"   ");
        }
        else if(key==KEY1_PRES)
        {
            if(g_freq > MIN_FREQ) g_freq -= FREQ_STEP; // 减小频率
            LCD_ShowxNum(70,230,g_freq,4,16,0);
            LCD_ShowString(114,230,200,16,16,"   ");
        }
        
        // 幅度调节
        if(key==KEY2_PRES)
        {
            if(g_amp < 3300) g_amp += 200; // 步进0.2V
            else g_amp = 0; // 达到最大值后回到最小值
            LCD_ShowxNum(70,250,g_amp/1000,1,16,0);
            LCD_ShowxNum(86,250,(g_amp%1000)/100,1,16,0);
            LCD_ShowString(102,250,200,16,16,"    ");
        }
        
        // 生成波形
        DAC1_Generate_Wave();
        
        // 显示更新
        if(t==10)
        {
            LED0=!LED0;
            t=0;
        }
        
        delay_ms(1); // 更快的更新速率以获得更好的波形
    }
}